package com.jn.rxjava.http;

import com.jn.rxjava.interceptor.FormParamsInterceptor;
import com.jn.rxjava.interceptor.HeaderInterceptor;
import com.jn.rxjava.interceptor.QueryParamsInterceptor;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class GlobalRetrofitClient {
    private static final String BASE_URL = "https://api.songzixian.com/api/";
    private static Retrofit retrofit;

    private static OkHttpClient getOkHttpClient() {
        return new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                // 添加全局请求头
                .addInterceptor(new HeaderInterceptor())
                // 添加全局Query参数
                .addInterceptor(new QueryParamsInterceptor())
                // 添加全局Form参数
                .addInterceptor(new FormParamsInterceptor())
                .build();
    }

    public static Retrofit getInstance() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .client(getOkHttpClient())
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                    .build();
        }
        return retrofit;
    }
}